package com.eshop.service;

import com.eshop.mapper.GoodMapper;
import com.eshop.mapper.OrderMapper;
import com.eshop.mapper.UserMapper;
import com.eshop.pojo.Good;
import com.eshop.pojo.Order;
import com.eshop.pojo.RespPageBean;
import com.eshop.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    GoodService goodService;

    //显示订单列表-商家使用
    public List<Order> getOrderList(){
        return orderMapper.getOrderList();
    }

    //添加订单-客户添加订单使用
    public int addOrder(Order order){
        order.setOCreationDate(new Date());
        User user = userMapper.queryUserByNickName(order.getUName());
        Good good = goodService.queryGoodById(order.getGId());
//        System.out.println("当前商品库存为："+good.getGCount());
        //若库存小于等于1则该商品将自动冻结
        if (good.getGCount()==1){
            good.setGIsFreeze(1);
            goodService.updateById(good);
        }
        order.setUId(user.getId());
        order.setOIsFinished(0);
        return orderMapper.addOrder(order);
    }
    //分页获得所有待处理订单列表
    public RespPageBean getOrderListByPage(Integer currentPage, Integer size, Order order) {
        PageHelper.startPage(currentPage,size);
        List<Order> orderList =  orderMapper.getOrderListByPage(order);
        PageInfo<Order> data = new PageInfo<Order>(orderList);
        //将数据封装到RespPageBean的实例对象bean中并返回给Controller层
        RespPageBean bean = new RespPageBean();
        bean.setData(data.getList());
        bean.setTotal(data.getTotal());
        return bean;
    }
    //分页获得所有历史订单列表
    public RespPageBean getHistoryOrderByPage(Integer currentPage, Integer size, Order order) {
        PageHelper.startPage(currentPage,size);
        List<Order> orderList =  orderMapper.getHistoryOrderByPage(order);
        PageInfo<Order> data = new PageInfo<Order>(orderList);
        //将数据封装到RespPageBean的实例对象bean中并返回给Controller层
        RespPageBean bean = new RespPageBean();
        bean.setData(data.getList());
        bean.setTotal(data.getTotal());
        return bean;
    }

    //分页获取当前顾客历史订单列表
    public RespPageBean getHistoryOrderByPageC(Integer currentPage, Integer size, Order order) {
        PageHelper.startPage(currentPage,size);
        List<Order> orderList =  orderMapper.getHistoryOrderByPageC(order);
        PageInfo<Order> data = new PageInfo<Order>(orderList);
        //将数据封装到RespPageBean的实例对象bean中并返回给Controller层
        RespPageBean bean = new RespPageBean();
        bean.setData(data.getList());
        bean.setTotal(data.getTotal());
        return bean;
    }
    //分页获取当前顾客所有订单列表
    public RespPageBean getHistoryOrderByPageCA(Integer currentPage, Integer size, Order order) {
        PageHelper.startPage(currentPage,size);
        List<Order> orderList =  orderMapper.getHistoryOrderByPageCA(order);
        PageInfo<Order> data = new PageInfo<Order>(orderList);
        //将数据封装到RespPageBean的实例对象bean中并返回给Controller层
        RespPageBean bean = new RespPageBean();
        bean.setData(data.getList());
        bean.setTotal(data.getTotal());
        return bean;
    }
    //分页获取当前顾客待处理订单列表
    public RespPageBean getOrderListByPageC(Integer currentPage, Integer size, Order order) {
        PageHelper.startPage(currentPage,size);
        List<Order> orderList =  orderMapper.getOrderListByPageC(order);
        PageInfo<Order> data = new PageInfo<Order>(orderList);
        //将数据封装到RespPageBean的实例对象bean中并返回给Controller层
        RespPageBean bean = new RespPageBean();
        bean.setData(data.getList());
        bean.setTotal(data.getTotal());
        return bean;
    }

    //订单交易成功
    public int OrderSuccess(int id) {
        return orderMapper.OrderSuccess(id);
    }

    //订单交易失败
    public int OrderFail(int id) {
        return orderMapper.OrderFail(id);
    }

    //删除历史订单
    public int deteleHistoryOrderById(int id) { return orderMapper.deteleHistoryOrderById(id); }

    //卖家发货
    public int Delivered(int id) {
        return orderMapper.Delivered(id);
    }

    public int cancelOrder(int id) { return orderMapper.cancelOrder(id);}
}
